<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  include('base.php');

  class PlayerController extends BaseController
  {
    var $_name = 'player';
  
    function display()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView('player', $document->getType());
      
      if ($model = & $this->getModel('player')) 
        $view->setModel($model, true);

      $view->displayList();
    }
    
    function edit()
    {
      $cids = JRequest::getVar('cid', null, 'default', 'array' );
      
      if($cids === null)
          JError::raiseError(500, 'cid parameter missing from the request');
      
      $document =& JFactory::getDocument();
    
      $view = & $this->getView('player', $document->getType());
      
      $view->setModel($this->getModel('player'), true);
      $view->setModel($this->getModel('person','personmodel'));
      $view->setModel($this->getModel('team','teammodel'));
      $view->setModel($this->getModel('position','positionmodel'));
      $view->setModel($this->getModel('pic','picmodel'));
      
      $view->setModel($this->getModel('season', 'seasonmodel'));
      $view->setModel($this->getModel('seasondivision','seasondivisionmodel'));
      $view->setModel($this->getModel('division','divisionmodel'));
      $view->setModel($this->getModel('divisionconference','divisionconferencemodel'));
      $view->setModel($this->getModel('conference','conferencemodel'));
      $view->setModel($this->getModel('conferenceteam','conferenceteammodel'));
      
      $view->displayEdit();
    }
    
    function save()
    {
      $mpic      = $this->getModel('pic','picmodel');
      $mplayer   = & $this->getModel('player');
      
      $data = JRequest::get('POST');
      
      #player
      $d_player = $this->reconstruct_player($data);
      $row      = $mplayer->save($d_player);
      $d_player['id'] = $row->id;
      
      #feature pic
      $f_pic           = $this->reconstruct_pic($data, 'feature_pic');
      $f_pic['pic_id'] = $row->id;
	  $mpic->save($f_pic);
      
      #profile
      $d_pic           = $this->reconstruct_pic($data, 'pic');
      $d_pic['pic_id'] = $row->id;
	  $row             = $mpic->save($d_pic);
      
      # update d_player
      $d_player['pic_id'] = $row->id;
      $mplayer->save($d_player);
      
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo, 'Player Saved!');             
    }
    
    function add()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView('player', $document->getType());
      $view->setModel($this->getModel('player'), true);
      $view->setModel($this->getModel('person','personmodel'));
      $view->setModel($this->getModel('team','teammodel'));
      $view->setModel($this->getModel('position','positionmodel'));
      $view->setModel($this->getModel('pic','picmodel'));
      
      $view->setModel($this->getModel('season', 'seasonmodel'));
      $view->setModel($this->getModel('seasondivision','seasondivisionmodel'));
      $view->setModel($this->getModel('division','divisionmodel'));
      $view->setModel($this->getModel('divisionconference','divisionconferencemodel'));
      $view->setModel($this->getModel('conference','conferencemodel'));
      $view->setModel($this->getModel('conferenceteam','conferenceteammodel'));
      
      $view->displayAdd();  
    }
    
    function remove()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array' ); 
 
	  if($ids === null)
	    JError::raiseError(500, 'cid parameter missing from the request');
	 
 
	  $model = & $this->getModel($this->_name);
	  $model->delete($ids);
 
	  $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
	  $this->setRedirect($redirectTo, ucfirst($this->_name) . '(s) Deleted!');                
    }    
    
    function publish()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array' ); 
       
      $model = & $this->getModel($this->_name); 
      $model->publish($ids, 1);
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo, ucfirst($this->_name) . '(s) Published!');   
    }
    
    function unpublish()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array' ); 
       
      $model = & $this->getModel($this->_name); 
      $model->publish($ids, 0);
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo, ucfirst($this->_name) . '(s) Unpublished!');       
    }
    
    function get_team_instances(){
    
      $result = array();
      
      $division_id = JRequest::getVar('division_id');
      $division_conference = $this->getModel('divisionconference', 'divisionconferencemodel');
      $conference_team     = $this->getModel('conferenceteam', 'conferenceteammodel');
      $team         = $this->getModel('team', 'teammodel');
      
      #loop for conferences
      $division_conferences = $division_conference->get_by_division_id($division_id);
      foreach($division_conferences as $dc){
        $conference_teams = $conference_team->get_by_conference_id($dc->conference_id);
        foreach($conference_teams as $ct)
          array_push($result,$team->get($ct->team_id));
      }
      
      $result  = JHTML::_('select.genericlist', $result, 'team_id', 'size='.count($result), 'id', 'name', null);
      echo $result;
      
    }
    
    private function reconstruct_player($data){

      $result = array();
      $result['id']           = $data['id'];
      $result['team_id']      = $data['team_id'];
      $result['person_id']    = $data['person_id'];
      $result['jersey_number']= $data['jersey_number'];
      $result['position_id']  = $data['position_id'];
      $result['date_start']   = $data['date_start'];
      $result['date_end']     = $data['date_end'];
      
      if($data['id'])
        $result['updated_on'] = date('Y-m-d',time());
      else
        $result['created_on'] = date('Y-m-d',time());
      
      return $result;
    }
    
    private function reconstruct_pic($data, $pic){
      
      $pic_id    = $pic.'_id';
      $pic_url   = $pic.'_url';
	  $flickr_id = $pic.'_flickr_id';
      
      $result = array();
      $result['id']          = $data[$pic_id];
      $result['description'] = basename($data[$pic_url]);
      $result['pic_type']    = 'PLAYERS';
      $result['pic_id']      = $data['id'];
      $result['flickr_id']   = $data[$flickr_id];
	  $result['url']         = $data['old_'.$pic_url];
	  $result['pic_url']     = $data[$pic_url];

      if($data[$pic_id])
        $result['updated_on'] = date('Y-m-d',time());
      else
        $result['created_on'] = date('Y-m-d',time());
      
      return $result;
    }
    
  }
?>